package com.ruoyi.syf.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.syf.common.constant.Check;
import com.ruoyi.common.enums.ConvertType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.syf.domain.RiskCheckDanger;
import com.ruoyi.syf.domain.RiskCheckDangerFix;
import com.ruoyi.syf.domain.vo.*;
import com.ruoyi.syf.mapper.RiskCheckDangerFixMapper;
import com.ruoyi.syf.service.IRiskCheckDangerDetectService;
import com.ruoyi.syf.service.IRiskCheckDangerFixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 隐患整改Service业务层处理
 *
 * @author yr
 * @date 2024-08-03
 */
@Service
public class RiskCheckDangerFixServiceImpl extends ServiceImpl<RiskCheckDangerFixMapper, RiskCheckDangerFix> implements IRiskCheckDangerFixService {

    @Autowired
    private IRiskCheckDangerDetectService riskCheckDangerDetectService;
    @Autowired
    private RiskCheckDangerFixMapper riskCheckDangerFixMapper;

    @ConvertField({
            @ConvertFieldItem(fieldName = "fixBy", assistFieldName = "fixByName", type = ConvertType.USER),
            @ConvertFieldItem(fieldName = "actualFixBy", assistFieldName = "actualFixByName", type = ConvertType.USER)
    })
    @Override
    public List<RiskCheckFixVo> list(RiskCheckFixVo vo) {
        vo.setLoginUserId(SecurityUtils.getUserId());
        return riskCheckDangerFixMapper.list(vo);
    }

    @ConvertField({
            @ConvertFieldItem(fieldName = "actualFixBy", assistFieldName = "actualFixByName", type = ConvertType.USER)
    })
    @Override
    public RiskCheckFixVo getInfo(Long id) {
        return riskCheckDangerFixMapper.getInfo(id);
    }

    @Override
    public FixBasicsVo getBasics(Long id) {
        return riskCheckDangerFixMapper.getBasics(id);
    }

    @ConvertField({
            @ConvertFieldItem(fieldName = "checkBy", assistFieldName = "checkByName", type = ConvertType.USER)
    })
    @Override
    public FixRecordVo getRecord(Long id) {
        return riskCheckDangerFixMapper.getRecord(id);
    }

    @ConvertField({
            @ConvertFieldItem(fieldName = "hazardDangerType", dictType = "danger_type", type = ConvertType.DICT),
            @ConvertFieldItem(fieldName = "dangerLever", constantValue = {"0:一般隐患", "1:重大隐患"}),
    })
    @Override
    public FixDangerVo getDanger(Long id) {
        return riskCheckDangerFixMapper.getDanger(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean confirm(RiskCheckDangerFix fix) {
        if (fix.getActualFixBy() == null){
            fix.setActualFixBy(SecurityUtils.getUserId());
        }
        fix.setFixStatus(Check.CheckInfo.F_SUCCEED.getCode());
        updateById(fix);
        //生成预验收信息
        riskCheckDangerDetectService.mySave(fix);
        return true;
    }

    /**
     * 批量删除隐患整改
     *
     * @param ids 需要删除的隐患整改主键
     * @return 结果
     */
    @Override
    public int deleteRiskCheckDangerFixByIds(Long[] ids) {
        return riskCheckDangerFixMapper.deleteRiskCheckDangerFixByIds(ids);
    }

    /**
     * 删除隐患整改信息
     *
     * @param id 隐患整改主键
     * @return 结果
     */
    @Override
    public int deleteRiskCheckDangerFixById(Long id) {
        return riskCheckDangerFixMapper.deleteRiskCheckDangerFixById(id);
    }

    @Override
    public boolean mySave(RiskCheckDanger danger) {
        RiskCheckDangerFix dangerFix = new RiskCheckDangerFix();
        dangerFix.setDangerId(danger.getId());
        dangerFix.setFixStatus(Check.CheckInfo.F_TO_CONFIRM.getCode());
        return save(dangerFix);
    }
}
